import { Toast } from 'antd-mobile'
import * as _ from 'lodash'
import * as Libs from '../../utils/Libs'
import * as assetLiabilityService from '../../services/home/assetLiabilityService'
import { createAction, NavigationActions } from '../../utils'

const initState = {
  assetLiabilitySummary: {
    zzc: {onRise: 0, showMoney: 0},
    fz: {onRise: 0, showMoney: 0},
    qy: {onRise: 0, showMoney: 0},
    times: ""
  },
  yj: "",
  gy: "",
  ASSET: [],
  ASSETSUMMARY: {},
  LIABILITY: [],
  LIABILITYSUMMARY: {},
  OWNER: [],
  LIA_OWNSUMMARY: {},
  zzc: {
    xAxisDatas: [],
    lineDatas: [],
    lastLineDatas: [],
    additionalD: []
  },
  fz: {
    xAxisDatas: [],
    lineDatas: [],
    lastLineDatas: [],
    additionalD: []
  },
  qy: {
    xAxisDatas: [],
    lineDatas: [],
    lastLineDatas: [],
    additionalD: []
  }
}

export default {
  namespace: 'assetliability',
  state: initState,
  reducers: {
    initState(state, { payload}) {
      return { ...initState}
    },
    initDetail(state, { payload }) {
      return { ...state,
        ASSET: [],
        ASSETSUMMARY: {},
        LIABILITY: [],
        LIABILITYSUMMARY: {},
        OWNER: [],
        LIA_OWNSUMMARY: {}
      }
    },
    initDetail(state, { payload}) {
      const timeD = payload.timeD || {}
      const details = {
        ASSET: [],
        ASSETSUMMARY: {},
        LIABILITY: [],
        LIABILITYSUMMARY: {},
        OWNER: [],
      }
      return { ...state, ...details}
    },
    changeState(state, { payload }) {
      return { ...state, ...payload }
    }
  },
  effects: {
    *getAssetLiabilityMainMsg({payload}, { call, select, put}) {
      console.log('main msg payload is ', payload)
      let params = {}
      params.startTime = (payload.startTime || "").replace('.','-')
      params.endTime = (payload.endTime || "").replace('.','-')
      const resp = yield call(assetLiabilityService.getAssetLiabilityMainMsg,params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respData = resp.data || []
      const zzc = respData['zzc'] || {}
      const zzc_SHOWMONEY = Libs.numberUpperFormat(zzc.sumMoney, true)
      const fz = respData['fz'] || {}
      const fz_SHOWMONEY = Libs.numberUpperFormat(fz.sumMoney, true)
      const qy = respData['qy'] || {}
      const qy_SHOWMONEY = Libs.numberUpperFormat(qy.sumMoney, true)
      yield put(createAction("changeState")({
        assetLiabilitySummary: {
          zzc: {onRise: zzc.onRise, showMoney: zzc_SHOWMONEY},
          fz: {onRise: fz.onRise, showMoney: fz_SHOWMONEY},
          qy: {onRise: qy.onRise, showMoney: qy_SHOWMONEY},
          times: respData.times || ""
        }
      }))
    },
    *getContentMsg({payload}, {call, select, put}) {
      let params = {}
      params.startTime = (payload.startTime || "").replace('.','-')
      params.endTime = (payload.endTime || "").replace('.','-')
      params.type = 1
      const resp = yield call(assetLiabilityService.getShouyeContentMsg, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respData = resp.data || {}
      const yj = (respData.yj || [])[0]
      const gy = (respData.gy || [])[0]
      console.log('yj ', yj, ' gy ', gy)
      yield put(createAction("changeState")({
        yj: yj,
        gy: gy
      }))
    },
    *getAssetLiabilityDetailMsg({payload}, { call, select, put}) {
      let params = {}
      params.startTime = (payload.startTime || "").replace('.','-')
      params.endTime = (payload.endTime || "").replace('.','-')
      const resp = yield call(assetLiabilityService.getAssetLiabilityDetailMsg, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      let _ASSET = []
      let _ASSETSUMMARY = {}
      let _LIABILITY = []
      let _LIABILITYSUMMARY = {}
      let _OWNER = []
      let _LIA_OWNSUMMARY = []
      const respData = resp.data || []

      //// 负债
      const fz = respData.fz || {}
      //负债总计
      const fzTotal = fz.total || {}
      _LIABILITYSUMMARY = assetLiabilityService.geneTitle("负债总计", fzTotal)
      //非流动负债
      const gdfz = fz.gdfz || {}
      const gdfzTotal = gdfz.total || {}
      const gdfzContent = gdfz.xq || []
      const _gdfzTitle = assetLiabilityService.geneTitle("非流动负债", gdfzTotal)
      const _gdfzContents = []
      _.map(gdfzContent, (v) => {
        _gdfzContents.push(assetLiabilityService.geneContent(v))
      })

      //流动负债
      const ldfz = fz.ldfz || {}
      const ldfzTotal = ldfz.total || {}
      const ldfzConent = ldfz.xq || []
      const _ldfzTitle = assetLiabilityService.geneTitle("流动负债", ldfzTotal)
      const _ldfzContents = []
      _.map(ldfzConent, (v) => {
        _ldfzContents.push(assetLiabilityService.geneContent(v))
      })
      _LIABILITY.push({
        title: _ldfzTitle,
        content: _ldfzContents
      })
      _LIABILITY.push({
        title: _gdfzTitle,
        content: _gdfzContents
      })

      ///// 资产

      const zc = respData.zc || {}
      //资产总计
      const zcTotal = zc.total || {}
      _ASSETSUMMARY = assetLiabilityService.geneTitle("资产总计", zcTotal)
      //非流动资产
      const gdzc = zc.gdzc || {}
      const gdzcTotal = gdzc.total || {}
      const gdzcContent = gdzc.xq || []
      const _gdzcTitle = assetLiabilityService.geneTitle("非流动资产", gdzcTotal)
      const _gdzcContents = []
      _.map(gdzcContent, (v) => {
        _gdzcContents.push(assetLiabilityService.geneContent(v))
      })

      //流动资产
      const ldzc = zc.ldzc || {}
      const ldzcTotal = ldzc.total || {}
      const ldzcConent = ldzc.xq || []
      const _ldzcTitle = assetLiabilityService.geneTitle("流动资产", ldzcTotal)
      const _ldzcContents = []
      _.map(ldzcConent, (v) => {
        _ldzcContents.push(assetLiabilityService.geneContent(v))
      })
      _ASSET.push({
        title: _ldzcTitle,
        content: _ldzcContents
      })
      _ASSET.push({
        title: _gdzcTitle,
        content: _gdzcContents
      })

      ///所有者权益
      const qy = respData.qy || {}
      const qqy = qy.qy ||{}
      const qqyTotal = qqy.total || {}
      const qqyContents = qqy.xq || []
      const _qqyTitle = assetLiabilityService.geneTitle("所有者权益", qqyTotal)
      const _qqyContents = []
      _.map(qqyContents, (v) => {
        _qqyContents.push(assetLiabilityService.geneContent(v))
      })
      _OWNER.push({
        title: _qqyTitle,
        content: _qqyContents
      })

      const ownqy = respData.fzandqy || {}
      _LIA_OWNSUMMARY = assetLiabilityService.geneTitle("负债和所有者权益总计", ownqy.total || {})
      yield put(createAction("changeState")({
        ASSET: _ASSET,
        ASSETSUMMARY: _ASSETSUMMARY,
        LIABILITY: _LIABILITY,
        LIABILITYSUMMARY: _LIABILITYSUMMARY,
        OWNER: _OWNER,
        LIA_OWNSUMMARY: _LIA_OWNSUMMARY,
      }))
    },
    *getAssetLiabilityModelCompare({payload}, { call, select, put}) {
      let params = {}
      params.startTime = (payload.startTime || "").replace('.','-')
      params.endTime = (payload.endTime || "").replace('.','-')
      const resp = yield call(assetLiabilityService.getAssetLiabilityModelCompare, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respData = resp.data
      const zzc = respData.zzc || []
      const _zzcxAxisDatas = []
      const _zzclineDatas = []
      const _zzcLastLineDatas = []
      const _zzcAdditionalDatas = []
      _.map(zzc, (v) => {
        _zzcxAxisDatas.push({
          time: v.yearMoth,
          tb: v.onRise,
          hb: v.linkRise
        })
        _zzclineDatas.push(v.sumMoney)
        _zzcLastLineDatas.push(v.tbSumMoney)
        _zzcAdditionalDatas.push({
          tb: v.onRise,
          hb: v.linkRise
        })
      })
      const fz = respData.fz || []
      const _fzxAxisDatas = []
      const _fzlineDatas = []
      const _fzLastLineDatas = []
      const _fzAdditionalDatas = []
      _.map(fz, (v) => {
        _fzxAxisDatas.push({
          time: v.yearMoth,
          tb: v.onRise,
          hb: v.linkRise
        })
        _fzlineDatas.push(v.sumMoney)
        _fzLastLineDatas.push(v.tbSumMoney)
        _fzAdditionalDatas.push({
          tb: v.onRise,
          hb: v.linkRise
        })
      })
      const qy = respData.qy || []
      const _qyxAxisDatas = []
      const _qylineDatas = []
      const _qyLastLineDatas = []
      const _qyAdditionalDatas = []
      _.map(qy, (v) => {
        _qyxAxisDatas.push({
          time: v.yearMoth,
          tb: v.onRise,
          hb: v.linkRise
        })
        _qylineDatas.push(v.sumMoney)
        _qyLastLineDatas.push(v.tbSumMoney)
        _qyAdditionalDatas.push({
          tb: v.onRise,
          hb: v.linkRise
        })
      })
      yield put(createAction("changeState")({
        zzc: {
          xAxisDatas: _zzcxAxisDatas,
          lineDatas: [_zzclineDatas, _zzcLastLineDatas],
          additionalD: _zzcAdditionalDatas
        },
        fz: {
          xAxisDatas: _fzxAxisDatas,
          lineDatas: [_fzlineDatas, _fzLastLineDatas],
          additionalD: _fzAdditionalDatas
        },
        qy: {
          xAxisDatas: _qyxAxisDatas,
          lineDatas: [_qylineDatas, _qyLastLineDatas],
          additionalD: _qyAdditionalDatas
        }
      }))
    }
  }
}
